package com.wuseguang.springcloud.service.impl;


import com.wuseguang.springcloud.dao.jdbc.QuestionnaireJdbc;
import com.wuseguang.springcloud.dao.repository.QuestionnaireRepository;
import com.wuseguang.springcloud.dao.repository.QuestionnaireUsersAnswerRepository;
import com.wuseguang.springcloud.entity.QuestionnaireUsersAnswer;
import com.wuseguang.springcloud.service.QuestionnaireService;
import com.wuseguang.springcloud.util.PaginationUtil;
import com.wuseguang.springcloud.util.PagingTool;
import com.wuseguang.springcloud.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 问卷调查表 服务实现类
 * </p>
 *
 * @author 
 * @since 2020-06-02
 */
@Service
public class QuestionnaireServiceImpl implements QuestionnaireService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private QuestionnaireRepository questionnaireRepository;
    @Autowired
    private QuestionnaireUsersAnswerRepository questionnaireUsersAnswerRepository;
    @Autowired
    private QuestionnaireJdbc questionnaireJdbc;

    @Override
    public Result findParentQuestionnaire(Integer studentId, Integer indexNo, Integer rows) {

        logger.info("家长查询问卷调查");
        //查询sql
        String sql = "select q.id,q.survey_name,q.survey_description,q.question_type,q.questionnaire_type, " +
                " q.created_by,qt.`questionnaire_option`,qt.title,qt.id questionnaire_title_id,qua.answer,qua.order_of_answers from questionnaire_users qu  " +
                " LEFT JOIN questionnaire q on(q.id=qu.questionnaire_id) " +
                " LEFT JOIN questionnaire_title qt on(qt.questionnaire_id=q.id) " +
                " LEFT JOIN questionnaire_users_answer qua on(qua.questionnaire_title_id=qt.id and qua.student_id=?) " +
                " where qu.student_id = ? and qu.questionnaire_type = 1 ORDER BY q.id";
        //家长的问卷导出
        List<Map<String,Object>> list = questionnaireJdbc.queryListMap(sql,studentId,studentId);

        //创建分页工具
        PagingTool tool = new PagingTool();
        //放入页码
        tool.setIndexNo(indexNo);
        //放入每页显示数量
        tool.setRows(rows);
        //放入角色集合
        tool.setList(list);
        //返回数据
        return new Result(new PaginationUtil<>(list.size(), rows, tool.getList(), indexNo));
    }

    @Override
    public Result addQuestionnaireUsersAnswer(QuestionnaireUsersAnswer questionnaireUsersAnswer) {
        logger.info("添加问卷调查信息");
        questionnaireUsersAnswerRepository.save(questionnaireUsersAnswer);
        return new Result();
    }
}
